import java.util.HashMap;
import java.util.Map;

public class Solution {
    public int[] searchRange(int[] nums, int target) {
        int[] ret = new int[2];
        ret[0] = ret[1] = -1;
        // 处理边界情况
        if(nums.length == 0) {
            return ret;
        }
        // 1. ⼆分左端点
        int left = 0;
        int right = nums.length - 1;
        while(left < right) {
            int mid = left + (right - left) / 2;
            if(nums[mid] < target) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        // 判断是否有结果
        if(nums[left] != target) {
            return ret;
        } else {
            ret[0] = right;
        }
        // 2. ⼆分右端点
        left = 0;
        right = nums.length - 1;
        while(left < right) {
            int mid = left + (right - left + 1) / 2;
            if(nums[mid] <= target) {
                left = mid;
            }else {
                right = mid - 1;
            }
        }
        ret[1] = left;
        return ret;
    }
    public int searchInsert(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] < target) {
                left = mid + 1;
            }
            else {
                right = mid;
            }
        }
        // 特判⼀下第三种情况
        if (nums[right] < target) {
            return right + 1;
        }
        return right;
    }
    public boolean wordPattern(String pattern, String s) {
        Map<Character,Integer> hash1 = new HashMap<>();
        Map<String,Integer> hash2 = new HashMap<>();
        String[] result = s.split(" ");
        char[] cp = pattern.toCharArray();
        if(cp.length != result.length) {
            return false;
        }
        for(int i = 0; i < cp.length;i++) {
            int a = hash1.getOrDefault(cp[i],0);
            int b = hash2.getOrDefault(result[i],0);
            if(a != b) {
                return false;
            } else {
                hash1.put(cp[i],a + 1);
                hash2.put(result[i],b + 1);
            }
            if(i < cp.length - 1) {
                Boolean x = (cp[i] == cp[i + 1]);
                Boolean y = (result[i].equals(result[i + 1]));
                if(x != y) {
                    return false;
                }
            }
        }
        return true;
    }
    public boolean wordPattern2(String pattern, String s) {
        Map<Character,Integer> hash1 = new HashMap<>();
        Map<String,Integer> hash2 = new HashMap<>();
        String[] result = s.split(" ");
        char[] cp = pattern.toCharArray();
        if(cp.length != result.length) {
            return false;
        }
        for(int i = 0; i < cp.length; i++) {
            int a = hash1.getOrDefault(cp[i],0);
            int b = hash2.getOrDefault(result[i],0);
            if(a != b) {
                return false;
            } else {
                hash1.put(cp[i],a + 1);
                hash2.put(result[i],b + 1);
            }
            if(i < cp.length - 1) {
                Boolean x = (cp[i] == cp[i + 1]);
                Boolean y = (result[i].equals(result[i + 1]));
                if(x != y) {
                    return false;
                }
            }
        }
        return true;
    }
}
